<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="6" :xs="24">
        <div class="app-container-tree-border">
          <el-input v-model="filterText" placeholder="请输入" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
          <el-tree :data="treeList" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id"
            highlight-current @node-click="handleNodeClick" :filter-node-method="filterNode"
            :default-expanded-keys="defaultExpandedKeys">
            <span slot-scope="{ node, data }" class="el-tree-custom-node">
              <span class="node-label" :title="node.label">
                {{ node.label }}
              </span>
              <span class="can-remove" v-if="data.type == 'fbfx'">
                <el-button type="text" size="mini" v-hasPermi="['base:bsFbfx:edit']"
                  @click.native.stop="onUpdateFbfx(data)">
                  修改
                </el-button>
                <el-popconfirm title="您确定要删除该目录吗？" icon-color="red" class="m-r-xs" v-hasPermi="['base:bsFbfx:remove']"
                  @confirm="onDeleteFbfx(data)">
                  <el-button slot="reference" type="text" size="mini">
                    删除
                  </el-button>
                </el-popconfirm>
              </span>
            </span>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px" class="top-left">
            <el-form-item label="报验时间" prop="reportDate">
              <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                range-separator="～" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="审批状态" prop="approveStatus">
              <el-select v-model="queryParams.approveStatus" placeholder="请选择审批状态" @change="handleQuery">
                <el-option v-for="dict in dict.type.report_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row class="mb8 top-right-btn small-line-height">
            <el-button v-if="treeInfo.id && (treeInfo.type == 'zd' || treeInfo.type == 'fbfx') && dataList.length == 0"
              type="success" plain size="mini" @click="onAddFbfx" v-hasPermi="['base:bsFbfx:add']">新增分部分项</el-button>
            <el-button v-if="treeInfo && treeInfo.id && treeInfo.children.length == 0" type="primary" plain size="mini"
              @click="onAddGx" v-hasPermi="['cm:bsProcessInspection:add']">新增工序</el-button>
          </el-row>
          <el-table border stripe v-loading="loading" :data="dataList">
            <el-table-column label="序号" align="center" width="50px" type="index" />
            <el-table-column label="工序名称" align="center" property="name" min-width="140" show-overflow-tooltip />
            <el-table-column label="报验时间" align="center" property="inspectTime" min-width="140" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ parseTime(scope.row.inspectTime, '{y}-{m}-{d}') }}
              </template>
            </el-table-column>
            <el-table-column label="期数" align="center" property="qs" min-width="80" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.qs ? '第' + scope.row.qs + '期' : '' }}
              </template>
            </el-table-column>
            <el-table-column label="审批状态" align="center" property="approveStatus" min-width="120">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.report_status" :value="scope.row.approveStatus" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="220" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" @click="onView(scope.row)">详情</el-button>
                <el-button v-if="canOperation(scope.row)" size="mini" type="text" @click="onDelGx(scope.row)"
                  v-hasPermi="['cm:bsProcessInspection:remove']">删除</el-button>
                <el-button v-if="canOperation(scope.row)" size="mini" type="text" @click="dataMaintenance(scope.row)"
                  v-hasPermi="['cm:bsProcessInspection:edit']">资料维护</el-button>
                <el-button v-if="canOperation(scope.row)" size="mini" type="text" @click="onExamine(scope.row)"
                  v-hasPermi="['cm:bsProcessInspection:approve']">发起审批</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>
    <fbfx-add-or-update ref="fbfxAddOrUpdateRef" @callBack="getFbfxTree" :zdId="treeInfo.zdId" />
    <gx-add ref="gxAddRef" @callBack="getList" :zdId="treeInfo.zdId" />
    <gxby-add-or-update ref="gxbyAddOrUpdateRef" @callBack="getList" />
    <gxby-view-dialog ref="gxbyViewDialogRef" />
    <examine-and-approve ref="examineAndApproveRef" @callBack="getList" />
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { fbfxTreelist } from "@/api/pl/bsFbfx";
import { listBsProcessInspection, delBsProcessInspection, checkProcessInspection } from "@/api/pl/bsProcessInspection";
import { delBsFbfx } from "@/api/pl/bsFbfx";
import FbfxAddOrUpdate from './fbfx-add-or-update.vue';
import GxAdd from './gx-add.vue';
import GxbyAddOrUpdate from './gxby-add-or-update.vue';
import GxbyViewDialog from './gxby-view-dialog.vue';
import ExamineAndApprove from './examine-and-approve.vue';
export default {
  name: 'ProcessInspection',
  dicts: ['report_status'],
  components: {
    FbfxAddOrUpdate,
    GxAdd,
    GxbyAddOrUpdate,
    GxbyViewDialog,
    ExamineAndApprove
  },
  data () {
    return {
      treeInfo: {},
      treeList: [],
      defaultExpandedKeys: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      filterText: undefined,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined,
        status: undefined,
      },
      form: {},
      dataList: []
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      this.projectChangeResetQuery();
      // 系统顶部选中项目变化
      this.getFbfxTree();
    },
    filterText (val) {
      this.$refs.tree.filter(val);
    }
  },
  created () {
    this.getFbfxTree();
  },
  mounted () {
  },
  methods: {
    getFbfxTree () {
      fbfxTreelist({ projectId: this.sysProjectId }).then(response => {
        this.treeList = response.data;
        if (this.treeInfo && this.treeInfo.id) {
          this.defaultExpandedKeys = [this.treeInfo.parentId, this.treeInfo.id];
        }
        this.treeInfo = {};
      });
    },
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick (data) {
      if (data.type == 'zd' || data.type == 'fbfx') {
        this.treeInfo = data;
        this.queryParams.fbfxId = data.id;
        if (data.children.length > 0) {
          this.dataList = [];
        } else {
          this.handleQuery();
        }
      } else {
        this.treeInfo = {};
      }
    },
    projectChangeResetQuery () {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        bdId: undefined,
        zdId: undefined,
        parentId: undefined,
      };
      this.total = 0;
      this.dataList = [];
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      listBsProcessInspection(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
    },
    // 表单重置
    reset () {
      this.form = {
        projectId: undefined,
      };
      this.resetForm("form");
    },
    // 新增分部分项
    onAddFbfx () {
      const info = {
        parentId: this.treeInfo.id,
        id: undefined
      };
      this.$refs.fbfxAddOrUpdateRef.init(info);
    },
    // 修改分部分项
    onUpdateFbfx (row) {
      const { id } = row;
      const info = {
        parentId: undefined,
        id: id
      };
      this.$refs.fbfxAddOrUpdateRef.init(info);
    },
    // 删除分部分项
    onDeleteFbfx (row) {
      delBsFbfx(row.id).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getFbfxTree();
      });
    },
    // 新增工序
    onAddGx () {
      this.$refs.gxAddRef.init(this.treeInfo.id);
    },
    // 删除工序
    onDelGx (row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除工序报验编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delBsProcessInspection(ids);
      }).then(() => {
        this.loading = false;
        this.handleQuery();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    // 资料维护
    dataMaintenance (row) {
      this.$refs.gxbyAddOrUpdateRef.init(row.id);
    },
    onView (row) {
      this.$refs.gxbyViewDialogRef.init(row.id);
    },
    // 是否可操作
    canOperation (row) {
      return row.approveStatus == '00' || row.approveStatus == '33';
    },
    /** 发起审批 */
    async onExamine (row) {
      try {
        await checkProcessInspection(row.id);
        const projectInfo = this.sysProjectList.find(item => item.projectId == this.sysProjectId);
        const info = {
          projectId: this.sysProjectId,
          projectName: projectInfo.projectName,
          zdId: row.zdId,
          fbfxId: row.fbfxId,
          id: row.id,
          processInspectionName: row.name,
        };
        this.$refs.examineAndApproveRef.init(info);
      } catch (error) {
      }
    },
  },
};
</script>
<style lang="scss" scoped></style>
